package com.bytedance.frameworks.baselib.network.connectionclass;

import android.net.TrafficStats;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;

import com.bytedance.common.utility.Logger;

import java.util.concurrent.atomic.AtomicInteger;

public final class ConnectionSamplingClient {
    static final class Holder {
        public static final ConnectionSamplingClient sInstance = new ConnectionSamplingClient(ConnectionClassManager.Holder.sInstance);

    }

    final class SamplingHandler extends Handler {
        private final ConnectionSamplingClient mConnectionSamplingClient;

        public SamplingHandler(ConnectionSamplingClient arg1, Looper arg2) {
            super(arg2);
            this.mConnectionSamplingClient = arg1;
        }

        public final void handleMessage(Message arg5) {
            switch (arg5.what) {
                case 1: {
                    this.mConnectionSamplingClient.compute();
                    this.sendEmptyMessageDelayed(1, 1000);
                }
                break;
                default:
                    throw new IllegalArgumentException("Unknown what=" + arg5.what);
            }
        }
    }

    private final ConnectionClassManager mConnectionClassManager;
    private AtomicInteger mRunningFlag;
    private SamplingHandler mSamplingHandler;
    private HandlerThread mHandlerThread;
    private long mStartSamplingRT;
    private static long mLastTotalRxBytes = -1;

    private ConnectionSamplingClient(ConnectionClassManager connectionClassManager) {
        this.mConnectionClassManager = connectionClassManager;
        this.mRunningFlag = new AtomicInteger();
        this.mHandlerThread = new HandlerThread("ParseThread");
        this.mHandlerThread.start();
        this.mSamplingHandler = new SamplingHandler(this, this.mHandlerThread.getLooper());
    }

    public static ConnectionSamplingClient getInstance() {
        return ConnectionSamplingClient.Holder.sInstance;
    }

    public final void startSampling() {
        try {
            if (this.mRunningFlag.getAndIncrement() != 0) {
                return;
            }

            if (Logger.debug()) {
                Logger.d("ConnectionClassManager", "startSampling");
            }

            this.mSamplingHandler.sendEmptyMessage(1);
            this.mStartSamplingRT = SystemClock.elapsedRealtime();
        } catch (Throwable throwable) {
        }
    }

    public final void stopSampling() {
        try {
            if (this.mRunningFlag.decrementAndGet() != 0) {
                return;
            }

            if (Logger.debug()) {
                Logger.d("ConnectionClassManager", "stopSampling");
            }

            this.mSamplingHandler.removeMessages(1);
            this.compute();
            mLastTotalRxBytes = -1;
        } catch (Throwable v0) {
        }
    }

    protected final void compute() {
        long totalRxBytes = TrafficStats.getTotalRxBytes();
        long usedRxBytes = totalRxBytes - mLastTotalRxBytes;
        if (mLastTotalRxBytes >= 0) {
            synchronized (this) {
                long rt = SystemClock.elapsedRealtime();
                this.mConnectionClassManager.calculate(usedRxBytes, rt - this.mStartSamplingRT);
                this.mStartSamplingRT = rt;
            }
        }
        mLastTotalRxBytes = totalRxBytes;
    }
}

